473,471 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript function to encode everything?

I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). I could write a regexp, but the
environment I'm in won't allow replace(). I could write something
cumbersome using indexOf, but if there's something easier, like
a builtin function that encodes every character in the string, I'd
prefer that. I'm a newbie at js, so type slowly, please.

Jul 23 '05 #1
7 9544
Richard Trahan wrote:
I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). I could write a regexp, but the
environment I'm in won't allow replace(). I could write something
cumbersome using indexOf, but if there's something easier, like
a builtin function that encodes every character in the string, I'd
prefer that.
There is not.
I'm a newbie at js, so type slowly, please.


How many characters per minute are allowed in this newsgroup?
I cna tpye 500 wrods pre mnitue!!1 ;-)

Seriously, I'm afraid there is no easy solution, especially because the
available methods of your implementation are virtually unknown. Maybe
you are looking for this:

/**
* @author
* (C) 2001-2004 Thomas Lahn <st*******@PointedEars.de>
* Distributed under the GNU GPL v2.
* @partof
* http://pointedears.de/scripts/string.js
* @argument _ sText
* @argument string sReplaced
* @argument string sReplacement
* @return type string
*/
function replaceText(sText, sReplaced, sReplacement)
{
var result = "";
var t;
if (!sText && sText.constructor == String)
{
sText = this;
}

var sNewText = sText;
var a;
// alert(sText);
if (sText && sReplaced && sReplacement)
{
if (sText.replace)
{
sReplaced = sReplaced.replace(/\\/g, "\\\\");
/* Version 1.23.2002.4 bugfix: allows to replace \ with other
* strings, required for proper rxReplaced;
* Example (no quotes, no escaping):
* sReplaced (provided) "\\"
* sReplaced (evaluated) \
* sReplaced (replaced as formulated above) "\\\\"
* sReplaced (esc. in RegExp constructor) "\\\\"
* sReplaced (ev. in RegExp constructor) \\
* rxReplaced (with RegExp escaping) /\\/g
* rxReplaced (evaluated) all occurr. of \
*/
var rxReplaced = new RegExp(sReplaced, "g");
sText = sText.replace(rxReplaced, sReplacement);

result = sText;
}
else if (sText.split
&& (sReplaced = sReplaced.replace(/\\/g, "\\\\"))
&& (a = sText.split(sReplaced))
&& a.join)
{
result = a.join(sReplacement);
}
else
{
var i = sText.indexOf(sReplaced);

if (i > -1)
{
sNewText = sText.substring(0, i);
sNewText += sReplacement
+ replaceText(
sText.substring(i + sReplaced.length),
sReplaced,
sReplacement);
}

result = sNewText;
}
}

return result;
}

var f = this.encodeURI || this.escape || function dummy(s) { return s; };
var plusEscape = "%" + "+".charCodeAt(0).toString(16).toUpperCase();
var x = replaceText(f("foo"), "+", plusEscape);

This will use the return value of encodeURI() or escape(), if available,
to escape the usual characters (why reinvent the wheel?) which is passed
to replaceText() which uses two alternatives if String.prototype.replace()
is unavailable:

A) sText.split(sReplaced).join(sReplacement)
B) calling the method recursively until every occurrence was replaced

You wrote that String.prototype.replace() is not available. This method was
introduced in JavaScript 1.2, so your implementation may be of the level of
JavaScript 1.1. String.prototype.split() and Array.prototype.join() are
available there, so it is likely that your implementation will use
alternative A.
HTH

PointedEars
Jul 23 '05 #2
[Previous posting canceled because of flaws in the implementation]

Richard Trahan wrote:
I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). I could write a regexp, but the
environment I'm in won't allow replace(). I could write something
cumbersome using indexOf, but if there's something easier, like
a builtin function that encodes every character in the string, I'd
prefer that.
There is not.
I'm a newbie at js, so type slowly, please.


How many characters per minute are allowed in this newsgroup?
I cna tpye 500 wrods pre mnitue!!1 ;-)

Seriously, I'm afraid there is no easy solution, especially because the
available methods of your implementation are virtually unknown. Maybe
you are looking for this:

var f = this.encodeURI || this.escape || function dummy(s) { return s; };
var plusEscape = "%" + "+".charCodeAt(0).toString(16).toUpperCase();
var x = f("foo").split("+").join(plusEscape);

This will use the return value of encodeURI() or escape(), if available,
to escape the usual characters in "foo" (why reinvent the wheel?). Then
the "+" character is replaced with its escape sequence.

You wrote that String.prototype.replace() is not available. This method
was introduced in JavaScript 1.2, so your implementation may be of the
level of JavaScript 1.1. String.prototype.split() and
Array.prototype.join() are available there, so it is likely that this will
work.
HTH

PointedEars
Jul 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
Richard Trahan wrote:
I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). [...]


[...]
You wrote that String.prototype.replace() is not available. This method
was introduced in JavaScript 1.2, so your implementation may be of the
level of JavaScript 1.1.


Which makes me wonder why it understands then encodeURI()
as of ECMAScript 3. Are you sure about replace()?
PointedEars
Jul 23 '05 #4
Thomas 'PointedEars' Lahn wrote:
Which makes me wonder why it understands then encodeURI()
as of ECMAScript 3. Are you sure about replace()?

I thank you profusely for your assistance, so I guess I
owe you an explanation about the replace(). It's not a
matter of js version. It's going to execute within an
eBay ad. eBay software will not allow js in an ad that
can modify text in such a way that simple software can't
scan the new text for forbidden phrases.
That means no replace() and no <script> tags
with a src attribute, which is why the method in your
first post won't work, but the method in your second
post will probably be ok. Anyway, I'm using a workaround
until I understand js a bit more; instead of using ?x=y
syntax and CGI to unpack the lone argument,
I'm using ?arg syntax and using ENV{QUERY_STRING} in the
Perl script that reads the GET, in which case any
character at all can be passed (it's the CGI::param method
that throws away the plus sign).

I'm determined to defeat eBay's restriction. They have a
nasty habit of changing ad requirements almost weekly, which
causes scores of people to manually edit thousands of ads
because the ad content cannot be served remotely. One possibility
is to reduce everything to images, but the file sizes would
be too large for folks with dialups. Another possibility is
to encode text characters disguised as image bytes (steganography),
but I don't know if js can unpack an image into its bytes.
Do you know?

Jul 23 '05 #5
On Fri, 13 Aug 2004 08:34:41 GMT, Richard Trahan <rt*****@optonline.net>
wrote:

[snip]
Another possibility is to encode text characters disguised as imagebytes
(steganography), but I don't know if js can unpack an imageinto its
bytes.
Do you know?


No, it can't.

Back to the original encoding question...

Whilst encodeURI() won't escape a plus (+) symbol, encodeURIComponent()
will. As you acknowledge, a plus symbol is valid within a URI, globally,
but the components (scheme, host, path, etc.) do not allow them.

Of course, this is of little use with older browsers, but I thought it
worth mentioning.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #6
Richard Trahan wrote:
Thomas 'PointedEars' Lahn wrote:
Which makes me wonder why it understands then encodeURI()
as of ECMAScript 3. Are you sure about replace()?
I thank you profusely for your assistance,


I thank *you*. I seldom get such kind responses.
so I guess I owe you an explanation about the replace(). It's not a matter
of js version. It's going to execute within an eBay ad. eBay software will
not allow js in an ad that can modify text in such a way that simple
software can't scan the new text for forbidden phrases.
ACK
That means no replace() and no <script> tags with a src attribute, which
is why the method in your first post won't work,
It was flawed anyway which is why I finally canceled the posting.
but the method in your second post will probably be ok.
Hopefully.
Anyway, I'm using a workaround until I understand js a bit more; instead
of using ?x=y syntax and CGI to unpack the lone argument, I'm using ?arg
syntax and using ENV{QUERY_STRING} in the Perl script that reads the GET,
in which case any character at all can be passed (it's the CGI::param
method that throws away the plus sign).
If you have the choice, you should always prefer the server-side approach
(which can be server-side JS, too) above the client-side one since
client-side scripting can be restricted or disabled by the the user or not
even present, while you often have full control over your server-side app.
Client-side scripting comes in handy when roundtrips/traffic should be
reduced; it is nothing that should be relied upon on a Web site.
I'm determined to defeat eBay's restriction. They have a
nasty habit of changing ad requirements almost weekly, which
causes scores of people to manually edit thousands of ads
because the ad content cannot be served remotely. One possibility
is to reduce everything to images, but the file sizes would
be too large for folks with dialups.
Yes, please do not do that. I remember the Web was not as much fun when I
only had 56k dial-up a few months before because many sites were optimized
(I'd say pessimized[tm]) for broadband use. I effectively had to disable
all images (or have much time) for reasonable usage.
Another possibility is to encode text characters disguised as image bytes
(steganography), but I don't know if js can unpack an image into its
bytes. Do you know?


I'm afraid JS itself cannot, it needs a host object to do so. As the host
environment to provide such an object is the Web browser and there is more
than one of them, there is no way this can be reliably implemented on an
*Internet* Web site.
PointedEars
Jul 23 '05 #7
I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). I could write a regexp, but the
environment I'm in won't allow replace(). I could write something
cumbersome using indexOf, but if there's something easier, like
a builtin function that encodes every character in the string, I'd
prefer that.
There is not.


YES, there is : encodeURIComponent()
That's the "strong" version of encodeURI()

I Hope I am not too late
Florian Ho
-
fho

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: elsenraat_76 | last post by:
Hello! I was wondering if someone could help me out with a problem I'm having? I'm trying to input a javascript value into an anchor tag (from a function), but don't have an event to call the...
2
by: Chris | last post by:
I am new to these forums and hope I am in the right place! I am working on an ASP (3.0) page that displays hotel data from a recordset in a table. In the last column of each row, I want to...
1
by: Viktor Popov | last post by:
I have a javascript functions which are used for populating 2 select boxes. The first select box is used for continent names, the second for the countries in the contint. When I click on a...
3
by: Steven K | last post by:
Hello, Can you call a vb.net procedure inside a JavaScript function? For example I need to validate 20-30 fields using JavaScript and if everything flies, run a vb.net procedure: <script...
5
by: mk | last post by:
Greetings all - im new to this newsgroup so pls dont flame me :p I need some help! Please view the html below in a browser. Or goto this url -http://firebrain.co.uk/java-problem.htm (Assuming...
1
by: sdonohue | last post by:
I am not very good at DHTML, but I have a set of links that use DHTML and they work :) But now the customer wants to have one of my DHTML links call a javascript function when it is clicked on. ...
5
by: IchBin | last post by:
Not sure if I should post this to a javascript group or here. So I'll try here first since most here mix both in this group. I am trying to pass a php value into a JavaScript function that changes...
1
by: --[zainy]-- | last post by:
Hello Guyz.. I want to assign a varascript value to a session object in javascript function... like i have an image and onClick i am calling a function sort_(str).. Now i want to assign this...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.